import axios from 'axios'
import type { AxiosRequestHeaders } from 'axios/index'

const { BASEURL } = process.env
const instance = axios.create({ baseURL: BASEURL, timeout: 10000 })

instance.interceptors.request.use(
  (config) => {
    config.headers = {
      ...config.headers,
    } as AxiosRequestHeaders
    return { ...config }
  },
  (error) => Promise.reject(error)
)

instance.interceptors.response.use(
  (response) => {
    const { data } = response
    return data.data
  },
  (error) => {
    const { response } = error
    const { code } = response
    return Promise.reject(error)
  }
)

export default instance
